<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/9
 * Time: 16:06
 */

class qualityChecks extends MY_Controller{
    function __construct()
    {
        parent::__construct();
        //加载模型
        $this->load->model('qc_basis_model','QCBasis');
    }

    /**
     *初始化函数
     */
    public function index(){
        $this->ProductFaultList();
    }
    //-----------------------------------------产品故障-----------------------------------------
    /**
     * 产品故障列表
     */
    public function ProductFaultList(){
        $gp=gp();
        $gp['page']=$gp['page']?$gp['page']:1;
        $str="?keyword=".$gp['keyword'].'&category='.$gp['category'];
        $language=$this->uri->segment(1);//获取当前语言
        $typeData=$this->QCBasis->GetProductType();//获取产品类型
        //重构产品类型数组，用于中英文切换
        foreach($typeData as &$row){
            switch($language){
                case 'sc':
                    $row['name']=$row['name_ch'];
                    break;
                case 'english':
                    $row['name']=$row['name_en'];
                    break;
                default:
                    $row['name']=$row['name_en'];
                    break;
            }
        }
        //分页查询
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url('qualityChecks/qualityChecks/ProductFaultList'.$str);
        $config['total_rows'] = $this->QCBasis->CountProductFault($gp['category'],$gp['keyword']);
        $config['uri_segment'] = config_item('enable_lang')?5:4;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $data['pageShow']= $this->pagination->create_links();
        //分页结束
        //获取产品故障
        $productFaultData=$this->QCBasis->GetFaultList($gp['category'],$gp['keyword'],$config['per_page'],($gp['page']-1)*$config['per_page']);
        //构建前端数据
        $data['productFaultData']=$productFaultData;
        $data['typeData']=$typeData;
        $this->load->view('defaults/qc_basis/qc_fault_list',$data);
        $this->load->view('defaults/common/footer',$data);
    }

    /**
     * 添加产品故障
     */
    public function AddProductFault(){
        $gp=gp();
        $data=array(
            'category'=>$gp['category'],
            'name_ch'=>$gp['name_ch'],
            'name_en'=>$gp['name_en'],
            'add_user'=>$this->users['name'],
            'add_time'=>time(),
        );
        $this->QCBasis->AddFault($data);
        $this->ProductFaultList();
    }

    /**
     * 更新产品故障
     */
    public function UpdateProductFault(){
        $gp=gp();
        $data=array(
            'category'=>$gp['category'],
            'name_ch'=>$gp['name_ch'],
            'name_en'=>$gp['name_en'],
            'add_user'=>$this->users['name'],
            'add_time'=>time(),
        );
        $this->QCBasis->UpdateFault($gp['id'],$data);
        $this->ProductFaultList();
    }

    /**
     * 删除产品故障
     */
    public function DeleteProductFault(){
        $gp=gp();
        $this->QCBasis->DeleteFault($gp['id']);
        $this->ProductFaultList();
    }
    //-----------------------------------------------产品附件-----------------------------------
    /**
     * 产品附件列表
     */
    public function ProductAccessoryList(){
        $gp=gp();
        $gp['page']=$gp['page']?$gp['page']:1;
        $str="?keyword=".$gp['keyword'].'&category='.$gp['category'];
        $language=$this->uri->segment(1);//获取当前语言
        $typeData=$this->QCBasis->GetProductType();//获取产品类型
        //重构产品类型数组，用于中英文切换
        foreach($typeData as &$row){
            switch($language){
                case 'sc':
                    $row['name']=$row['name_ch'];
                    break;
                case 'english':
                    $row['name']=$row['name_en'];
                    break;
                default:
                    $row['name']=$row['name_en'];
                    break;
            }
        }
        //分页查询
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url('qualityChecks/qualityChecks/ProductAccessoryList'.$str);
        $config['total_rows'] = $this->QCBasis->CountProductAccessory($gp['category'],$gp['keyword']);
        $config['uri_segment'] = config_item('enable_lang')?5:4;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $data['pageShow']= $this->pagination->create_links();
        //分页结束
        //获取产品故障
        $productFaultData=$this->QCBasis->GetAccessoryList($gp['category'],$gp['keyword'],$config['per_page'],($gp['page']-1)*$config['per_page']);
        //构建前端数据
        $data['productAccessoryData']=$productFaultData;
        $data['typeData']=$typeData;
        $this->load->view('defaults/qc_basis/qc_accessory_list',$data);
        $this->load->view('defaults/common/footer',$data);
    }

    /**
     * 添加产品附件
     */
    public function AddProductAccessory(){
        $gp=gp();
        $data=array(
            'category'=>$gp['category'],
            'name_ch'=>$gp['name_ch'],
            'name_en'=>$gp['name_en'],
            'add_user'=>$this->users['name'],
            'add_time'=>time(),
        );
        $this->QCBasis->AddAccessory($data);
        $this->ProductAccessoryList();
    }

    /**
     * 更新产品附件
     */
    public function UpdateProductAccessory(){
        $gp=gp();
        $data=array(
            'category'=>$gp['category'],
            'name_ch'=>$gp['name_ch'],
            'name_en'=>$gp['name_en'],
            'add_user'=>$this->users['name'],
            'add_time'=>time(),
        );
        $this->QCBasis->UpdateAccessory($gp['id'],$data);
        $this->ProductAccessoryList();
    }

    /**
     * 删除产品附件
     */
    public function DeleteProductAccessory(){
        $gp=gp();
        $this->QCBasis->DeleteAccessory($gp['id']);
        $this->ProductAccessoryList();
    }
}